home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 May / CMCD0504.ISO / Software / Freeware / Programare / gdiplusdelphi / demos / Using Text and Fonts / Obtaining Font Metrics / GDITEST55.dpr
Encoding:
Text File  |  2003-10-15  |  4.8 KB  |  151 lines

  1. program GDITEST55;
  2.  
  3. uses
  4.   Windows,
  5.   Messages,
  6.   SysUtils,
  7.   GDIPAPI,
  8.   GDIPOBJ;
  9.  
  10.  
  11. Procedure OnPaint(DC: HDC);
  12. var
  13.   graphics : TGPGraphics;
  14.   infoString: String;       // enough space for one line of output
  15.   ascent: UINT;             // font family ascent in design units
  16.   ascentPixel: Single;      // ascent converted to pixels
  17.   descent: UINT;            // font family descent in design units
  18.   descentPixel: Single;     // descent converted to pixels
  19.   lineSpacing: UINT;        // font family line spacing in design units
  20.   lineSpacingPixel: Single; // line spacing converted to pixels
  21.  
  22.   fontFamily: TGPFontFamily;
  23.   font: TGPFont;
  24.   pointF: TGPPointF;
  25.   solidBrush: TGPSolidBrush;
  26. begin
  27.   graphics := TGPGraphics.Create(DC);
  28.   fontFamily:= TGPFontFamily.Create('Arial');
  29.   font:= TGPFont.Create(fontFamily, 16, FontStyleRegular, UnitPixel);
  30.   pointF:= MakePoint(10.0, 10.0);
  31.   solidBrush:= TGPSolidBrush.Create(MakeColor(255, 0, 0, 0));
  32.  
  33.   // Display the font size in pixels.
  34.   infoString := format('font.GetSize returns %f.', [font.GetSize]);
  35.   graphics.DrawString(infoString, Length(infoString), font, pointF, solidBrush);
  36.  
  37.   // Move down one line.
  38.   pointF.Y := pointF.Y + font.GetHeight(0.0);
  39.  
  40.   // Display the font family em height in design units.
  41.   infoString := format('fontFamily.GetEmHeight returns %d.',
  42.     [fontFamily.GetEmHeight(FontStyleRegular)]);
  43.   graphics.DrawString(infoString, -1, font, pointF, solidBrush);
  44.  
  45.   // Move down two lines.
  46.   pointF.Y := pointF.Y + 2.0 * font.GetHeight(0.0);
  47.  
  48.   // Display the ascent in design units and pixels.
  49.   ascent := fontFamily.GetCellAscent(FontStyleRegular);
  50.  
  51.   // 14.484375 = 16.0 * 1854 / 2048
  52.   ascentPixel := font.GetSize * ascent / fontFamily.GetEmHeight(FontStyleRegular);
  53.   infoString := format('The ascent is %d design units, %f pixels.', [ascent, ascentPixel]);
  54.   graphics.DrawString(infoString, -1, font, pointF, solidBrush);
  55.  
  56.   // Move down one line.
  57.   pointF.Y := pointF.Y + font.GetHeight(0.0);
  58.  
  59.   // Display the descent in design units and pixels.
  60.   descent := fontFamily.GetCellDescent(FontStyleRegular);
  61.  
  62.   // 3.390625 = 16.0 * 434 / 2048
  63.   descentPixel := font.GetSize * descent / fontFamily.GetEmHeight(FontStyleRegular);
  64.   infoString := format('The descent is %d design units, %f pixels.', [descent, descentPixel]);
  65.   graphics.DrawString(infoString, -1, font, pointF, solidBrush);
  66.  
  67.   // Move down one line.
  68.   pointF.Y := pointF.Y + font.GetHeight(0.0);
  69.  
  70.   // Display the line spacing in design units and pixels.
  71.   lineSpacing := fontFamily.GetLineSpacing(FontStyleRegular);
  72.  
  73.   // 18.398438 = 16.0 * 2355 / 2048
  74.   lineSpacingPixel := font.GetSize * lineSpacing / fontFamily.GetEmHeight(FontStyleRegular);
  75.   infoString := format('The line spacing is %d design units, %f pixels.',
  76.     [lineSpacing, lineSpacingPixel]);
  77.   graphics.DrawString(infoString, -1, font, pointF, solidBrush);
  78.  
  79.   fontFamily.Free;
  80.   font.Free;
  81.   solidBrush.Free;
  82.   graphics.Free;
  83. end;
  84.  
  85.  
  86. function WndProc(Wnd : HWND; message : UINT; wParam : Integer; lParam: Integer) : Integer; stdcall;
  87. var
  88.   Handle: HDC;
  89.   ps: PAINTSTRUCT;
  90. begin
  91.   case message of
  92.     WM_PAINT:
  93.       begin
  94.         Handle := BeginPaint(Wnd, ps);
  95.         OnPaint(Handle);
  96.         EndPaint(Wnd, ps);
  97.         result := 0;
  98.       end;
  99.  
  100.     WM_DESTROY:
  101.       begin
  102.         PostQuitMessage(0);
  103.         result := 0;
  104.       end;
  105.  
  106.    else
  107.       result := DefWindowProc(Wnd, message, wParam, lParam);
  108.    end;
  109. end;
  110.  
  111. var
  112.   hWnd     : THandle;
  113.   Msg      : TMsg;
  114.   wndClass : TWndClass;
  115. begin
  116.    wndClass.style          := CS_HREDRAW or CS_VREDRAW;
  117.    wndClass.lpfnWndProc    := @WndProc;
  118.    wndClass.cbClsExtra     := 0;
  119.    wndClass.cbWndExtra     := 0;
  120.    wndClass.hInstance      := hInstance;
  121.    wndClass.hIcon          := LoadIcon(0, IDI_APPLICATION);
  122.    wndClass.hCursor        := LoadCursor(0, IDC_ARROW);
  123.    wndClass.hbrBackground  := HBRUSH(GetStockObject(WHITE_BRUSH));
  124.    wndClass.lpszMenuName   := nil;
  125.    wndClass.lpszClassName  := 'GettingStarted';
  126.  
  127.    RegisterClass(wndClass);
  128.  
  129.    hWnd := CreateWindow(
  130.       'GettingStarted',       // window class name
  131.       'Obtaining Font Metrics',       // window caption
  132.       WS_OVERLAPPEDWINDOW,    // window style
  133.       Integer(CW_USEDEFAULT), // initial x position
  134.       Integer(CW_USEDEFAULT), // initial y position
  135.       Integer(CW_USEDEFAULT), // initial x size
  136.       Integer(CW_USEDEFAULT), // initial y size
  137.       0,                      // parent window handle
  138.       0,                      // window menu handle
  139.       hInstance,              // program instance handle
  140.       nil);                   // creation parameters
  141.  
  142.    ShowWindow(hWnd, SW_SHOW);
  143.    UpdateWindow(hWnd);
  144.  
  145.    while(GetMessage(msg, 0, 0, 0)) do
  146.    begin
  147.       TranslateMessage(msg);
  148.       DispatchMessage(msg);
  149.    end;
  150. end.
  151.